package com.longxi.tianjintianbao.ui.presenter.assetscheck;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

import com.baiiu.filter.interfaces.OnFilterDoneListener;
import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.activity.assetscheck.AssetsDetailActivity;
import com.longxi.tianjintianbao.ui.activity.scan.ScanActivity;
import com.longxi.tianjintianbao.ui.adapter.AssetsPagerAdapter;
import com.longxi.tianjintianbao.ui.adapter.list.ConstellationAdapter;
import com.longxi.tianjintianbao.ui.adapter.list.DropMenuAdapter;
import com.longxi.tianjintianbao.ui.adapter.list.entity.FilterUrl;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.assetscheck.IAssetsContentAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.ArrayList;
import java.util.List;

public class AssetsContentAtPresenter extends BasePresenter<IAssetsContentAtView> implements OnFilterDoneListener {

    //tab 标签页
    private List<String> mCategoryList;
    private AssetsPagerAdapter myAssetsPagerAdapter;
    private String headers[] = {"房栋", "单元", "楼层"};
    private String house[] = {"01栋", "02栋", "03栋", "04栋", "05栋", "06栋", "07栋", "08栋", "09栋", "10栋", "11栋", "12栋"};
    private String unit[] = {"一单元", "二单元", "三单元", "四单元", "五单元", "六单元", "七单元", "八单元", "九单元", "十单元", "十一单元", "十二单元"};
    private String floor[] = {"一层", "二层", "三层", "四层", "五层", "六层", "七层", "八层"};
    private List<View> popupViews = new ArrayList<>();

    ConstellationAdapter houseAdapter;
    ConstellationAdapter unitAdapter;
    ConstellationAdapter floorAdapter;

    private void initVariable() {
        mCategoryList = new ArrayList<>();
        mCategoryList.add("全部");
        mCategoryList.add("未盘点");
        mCategoryList.add("已盘点");
        mCategoryList.add("正常");
        mCategoryList.add("异常");
    }

    private void initFilterDropDownView() {
        getView().getDropDownMenu().setMenuAdapter(new DropMenuAdapter(mContext, headers, this));
    }

//    private void initDropDownMenu() {
//        //房栋
//        final View houseView = mContext.getLayoutInflater().inflate(R.layout.custom_layout, null);
//        GridView houseGrid = houseView.findViewById(R.id.constellation);
////        houseGrid.setBackgroundColor(MyUtils.getResource().getColor(R.color.bg));
//        houseAdapter = new ConstellationAdapter(mContext, Arrays.asList(house));
//        houseGrid.setAdapter(houseAdapter);
//        //单元
//        final View unitView = mContext.getLayoutInflater().inflate(R.layout.custom_layout, null);
//        GridView unitGrid = unitView.findViewById(R.id.constellation);
////        unitGrid.setBackgroundColor(MyUtils.getResource().getColor(R.color.bg));
//        unitAdapter = new ConstellationAdapter(mContext, Arrays.asList(unit));
//        unitGrid.setAdapter(unitAdapter);
//        //楼层
//        final View floorView = mContext.getLayoutInflater().inflate(R.layout.custom_layout, null);
//        GridView floorGrid = floorView.findViewById(R.id.constellation);
//        floorGrid.setBackgroundColor(MyUtils.getResource().getColor(R.color.bg));
//        floorGrid.setPadding(0,0,0,0);
//        floorAdapter = new ConstellationAdapter(mContext, Arrays.asList(floor));
//        floorGrid.setAdapter(floorAdapter);
//
//        popupViews.add(houseView);
//        popupViews.add(unitView);
//        popupViews.add(floorView);
//
//        //add item click event
//        houseGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                houseAdapter.setCheckItem(position);
////                getView().getDropDownMenu().setTabText(position == 0 ? headers[0] : house[position]);
////                getView().getDropDownMenu().closeMenu();
//            }
//        });
//
//        unitGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                unitAdapter.setCheckItem(position);
////                getView().getDropDownMenu().setTabText(position == 1 ? headers[1] : unit[position]);
////                getView().getDropDownMenu().closeMenu();
//            }
//        });
//
//        floorGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                floorAdapter.setCheckItem(position);
////                getView().getDropDownMenu().setTabText(position == 2 ? headers[2] : floor[position]);
////                getView().getDropDownMenu().closeMenu();
//            }
//        });
//
//        //init context view
//        View contentView = new TextView(mContext);
//        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
////        contentView.setText("内容显示区域");
////        contentView.setGravity(Gravity.CENTER);
////        contentView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
//
////        getView().getDropDownMenu().setDropDownMenu(Arrays.asList(headers), popupViews, contentView);
//
//    }

    public List<String> getCategoryList() {
        if (mCategoryList == null) {
            mCategoryList = new ArrayList<>();
        }
        return mCategoryList;
    }

    public AssetsContentAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        mContext.setTitle("资产盘点");
        mContext.setTvLeft(MyUtils.getString(R.string.back));
        mContext.setIvRight(R.mipmap.ic_scan);
        initVariable();
        initFilterDropDownView();
        initViewPager();
    }

    public void initViewPager(){
        myAssetsPagerAdapter = new AssetsPagerAdapter(mContext.getSupportFragmentManager(), getCategoryList());
        getView().getViewPager().setAdapter(myAssetsPagerAdapter);
        getView().getViewPager().setOffscreenPageLimit(getCategoryList().size() - 1);
        getView().getTabLayout().setViewPager(getView().getViewPager());
    }

    @Override
    public void onFilterDone(int position, String positionTitle, String urlValue) {
        if (position != 3) {
            getView().getDropDownMenu().setPositionIndicatorText(FilterUrl.instance().position, FilterUrl.instance().positionTitle);
        }

        getView().getDropDownMenu().close();
        MyUtils.showToast(FilterUrl.instance()
                .toString());
    }

    public void scan() {
        AndPermission.with(mContext)
        .permission(Permission.CAMERA, Permission.READ_EXTERNAL_STORAGE)
        .onGranted(new Action() {
            @Override
            public void onAction(List<String> permissions) {
                Intent intent = new Intent(mContext, ScanActivity.class);
                mContext.startActivityForResult(intent, AppConst.IntentRequstCode.REQUEST_CODE_SCAN);
            }
        })
        .onDenied(new Action() {
            @Override
            public void onAction(List<String> permissions) {
                Uri packageURI = Uri.parse("package:" + mContext.getPackageName());
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                mContext.startActivity(intent);

                Toast.makeText(mContext, "没有权限无法扫描呦", Toast.LENGTH_LONG).show();
            }
        }).start();
    }

    public void onActivityResult(int requestCode, Intent data) {
        switch (requestCode) {
            case AppConst.IntentRequstCode.REQUEST_CODE_SCAN:
                if (data != null) {
                    Bundle bundle = data.getExtras();
                    String result = bundle.getString(AppConst.RESULT_STRING);
                    MyUtils.showToast(result);
                    Intent intent = new Intent(mContext, AssetsDetailActivity.class);
                    mContext.startActivity(intent);
//                    if (null != data) {
//                        Bundle bundle = data.getExtras();
//                        if (bundle == null) {
//                            return;
//                        }
//                        if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
//                            String result = bundle.getString(CodeUtils.RESULT_STRING);
//                            String key = "SB_";
//                            if(!result.contains(key)){
//                                MyUtils.showToast("该二维码不是设备二维码");
//                                return;
//                            }
//
//                            result = result.replace(key, "");
//
//                            try {
//                                LogUtils.d("二维码结果", result);
//                                int a = Integer.parseInt(result);
//                                Intent intent = new Intent(mContext, EquipmentDetailsActivity.class);
//                                intent.putExtra(EquipmentDetailsAtPresenter.EQUIPMENTID, result);
//                                mContext.startActivity(intent);
//                            } catch (NumberFormatException e) {
//                                e.printStackTrace();
//                                MyUtils.showToast("该二维码不是巡检二维码");
//                            }
//                        } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
//                            MyUtils.showToast("解析二维码失败");
//                        }
//                    }
                }
                break;
        }
    }
}
